﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sms_Send_log_Add.aspx.cs" Inherits="JKF.Web.Communication.SMS.Sms_Send_log_Add" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <%= RegScript("~/js/jquery.1.5.js")%>
    <%=RegScript("~/js/Common.js")%>
    <%=RegScript("~/js/jquery.json.js")%>
    <%=RegScript("~/js/jquery.nhis.js")%>
    <%=RegScript("~/js/jquery.rchis.js")%>
    <%=RegScript("~/js/jquery.blockUI.js")%>
    <%=RegScript("~/js/jquery.validate.js")%>
    <%=RegScript("~/js/DatePicker/WdatePicker.js")%>
    <%=RegStyle("~/js/suggest/jquery.suggest.css")%>
    <%=this.RegStyle("~/App_Themes/Theme2/Include.css")%>
    <script type="text/javascript">
        var account=<%=CurrentAccount%>;        
        var appPath = "<%=AppPath %>";
        var tmpl = new DTemplate();
        var areaHandler = appPath + '/do/Dic/Dic_Area.do';
        var handler=appPath+"/do/SMS/SmsModule.do";
        var customerHandler=appPath+"/do/Customer/Customer.do";
        var dtReg = /^\d{4}(\/|-)([1][0-2]|[1-9]|0[1-9])(\/|-)([1-2][0-9]|3[0-1]|[1-9]|0[1-9])( (0?[0-9]|1[0-9]|2[0-4])(:([0-5][0-9])){1,2})?$/;
        var mxChar = 0;
        var sendNow = <%=PSendNow%>;
        var sendByTime = <%=PSendByTime%>;
        
        $(function() {
            BindAccInfo();
            RegisterHandler();
            InitPage();
            $("#ByTimeStyle").change();
            smsSendType(!$("#sendNow").attr("checked"));
            
            //SendNow
            if (!sendNow) {
                $("#SendNow").remove();
                $("#SendByTime").checked();
                $("#SendByTime").change();
            }
            if (!sendByTime) {
                $("#SendByTime").remove();
            }
        });

        function ClearForm() {
            UpdateAccInfo();
            $(":text").val("");
            $("select").val("");
            $("textarea").val("");
            $("#SCOPEPson").attr("checked","checked");
            $("#SendNow").attr("checked","checked");
            smsSendType(true);
            $("#CONTENT").change();
        }

        function RegisterHandler() {
            $("#SCOPEPson").change(function() {chgPsonSelect(false);});
            $("#SCOPECondition").change(function() {chgPsonSelect(true);});
            $("#SendNow").change(function() {smsSendType(true);});
            $("#SendByTime").change(function() {smsSendType(false);});
            $("#ByTimeStyle").change(ShowTimerSelector);
            $("#btnSelectMobile").click(function() {window.parent.pop("选择手机号", '<%= ResolveUrl("~/SMS/Sms_Select_Mobile.aspx") %>' , 900, 500,smsCallback);});
           
            $("#btnSave").click(SaveTask);
            $("#SMS_TEMPLATE").change(function (){
                var vl=$.trim($(this).find("option[value='"+$(this).val()+"']").attr("cont"));
                if(vl.length>0){$("#CONTENT").val(vl);}
                $("#CONTENT").change();
            });
            $("#wordNumLeft").text(250-mxChar);
            $("#CONTENT").keyup(function() {
                $("#wordNum").text($("#CONTENT").val().length);
                $("#wordNumLeft").text(250-mxChar-$("#CONTENT").val().length);
            }).change(function() {$(this).keyup();}).attr("maxlength",250-mxChar);

            alxVr.push({
                id:"MOBILE_NUMS",
                rule:function (cn){
                    if($("#SCOPEPson").attr("checked")){
                        var vl=$.trim(cn.val());
                        if(vl.length==0){
                            this.msg="请选择手机号.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"CONDITION_KEY",
                rule:function (cn){
                    if($("#SCOPECondition").attr("checked")){
                        var vl=$.trim(cn.val());
                        if(vl.length==0){
                            this.msg="请选择条件.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"CONTENT",
                rule:function (cn){
                    var vl=$.trim(cn.val());
                    var sign = $.trim($("#signature").text());
                    if(vl.length==0){
                        this.msg="请选输入短信内容.";
                        return false;
                    }else if (vl.length+sign.length > 250) {
                        this.msg = "短信内容加签名的长度应在250个字符之内.";
                        return false;
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"Date",
                rule:function(cn){
                    if($("#SendByTime").attr("checked") && $("#ByTimeStyle").val()=="0"){
                        var vl=$.trim(cn.val());
                        if(!dtReg.test(vl)){
                            this.msg="日期格式不正确.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"Day",
                rule:function(cn){
                    var vl=$.trim(cn.val());
                    if($("#SendByTime").attr("checked") && $("#ByTimeStyle").val()=="3"){
                        if(!/^\d+$/.test(vl)){
                            this.msg="请选择日期.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"Week",
                rule:function(cn){
                    var vl=$.trim(cn.val());
                    if($("#SendByTime").attr("checked")  && $("#ByTimeStyle").val()=="2"){
                        if(vl==""){
                            this.msg="请选择星期.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"Hour",
                rule:function(cn){
                    var vl=$.trim(cn.val());
                    if($("#SendByTime").attr("checked")){
                        if(!/^\d+$/.test(vl)){
                            this.msg="请选择小时.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            alxVr.push({
                id:"Minute",
                rule:function(cn){
                    var vl=$.trim(cn.val());
                    if($("#SendByTime").attr("checked")){
                        if(!/^\d+$/.test(vl)){
                            this.msg="请选择分钟.";
                            return false;
                        }
                    }
                    return true;
                }
            });
            $.validateBind();
        }

        function smsCallback(mobiles) {
            $("#MOBILE_NUMS").val(mobiles.join(";"));
        }
        
        function chgPsonSelect(bCondit) {
            if (bCondit){ 
                $("#CONDITION_KEY").removeAttr("disabled");
                $("#MOBILE_NUMS").attr("disabled","disabled");
                $("#btnSelectMobile").attr("disabled","disabled");
            }
            else {
                $("#CONDITION_KEY").attr("disabled","disabled");
                $("#MOBILE_NUMS").removeAttr("disabled");
                $("#btnSelectMobile").removeAttr("disabled");
            }
        }

        function smsSendType(bCurr) {
            if (bCurr) {
                $("#ByTimeStyle").attr("disabled","disabled").css("display","none");
                $("#ByTimeStyle").val("0");
                $("#DateTimeStyle").find("span").css("display","none");
                $("#btnSave").addClass("btn_fasong2");
                $("#btnSave").removeClass("common_btn_save");
            } else {
                $("#ByTimeStyle").removeAttr("disabled").css("display","");
                ShowTimerSelector();
                $("#btnSave").addClass("common_btn_save");
                $("#btnSave").removeClass("btn_fasong2");
            }
        }

        function InitPage() {
            var wk = ["一", "二", "三", "四", "五", "六", "日"];
            var day = $("#Day");
            var week = $("#Week");
            var hour = $("#Hour");
            var minute = $("#Minute");
            for (var i = 1; i < 32; i++) day.append($("<option></option>").text(i).attr("value", i));
            for (var i = 0; i < wk.length; i++) week.append($("<option></option>").text("星期" + wk[i]).attr("value","星期" + wk[i]));
            for (var i = 0; i < 24; i++) hour.append($("<option></option>").text(i).attr("value", i));
            for (var i = 0; i < 60; i++) minute.append($("<option></option>").text(i).attr("value", i));
        }

        function ShowTimerSelector() {
            var vl=$.trim($("#ByTimeStyle").val());
            $("#DayCt").css("display", "none");
            $("#DateCt").css("display", "none");
            $("#WeekCt").css("display", "none");
            $("#HourCt").css("display", "none");
            $("#MinuteCt").css("display", "none");
            
            switch (vl) {
                case "0":
                    $("#DateCt").css("display","");
                    $("#HourCt").css("display","");
                    $("#MinuteCt").css("display","");
                    break;
                case "1":
                    $("#HourCt").css("display","");
                    $("#MinuteCt").css("display","");
                    break;
                case "2":
                    $("#WeekCt").css("display","");
                    $("#HourCt").css("display","");
                    $("#MinuteCt").css("display","");
                    break;
                case "3":
                    $("#DayCt").css("display","");
                    $("#HourCt").css("display","");
                    $("#MinuteCt").css("display","");
            }
        }

        function UpdateAccInfo() {
            $.post(handler, {ActionName:"GetSmsAccountInfo"}, function(rslt) {
                if (rslt.Success) {
                    account = rslt.data;
                    BindAccInfo();
                    return;
                }
                alert(rslt.Message);
            },"json");
        }

        function BindAccInfo() {
            if (!account) {
                $("input,select,textarea").attr("disabled","disabled");
                $("#NoAccountMsg").text("提示：未开通短信发送功能，无法使用，如要开通请联系管理员.").css("height","auto").css("color","#FF0000");
                return;
            }
            $("#curInfo").html("可发送短信<span class=\"f12red\">"+account.NUM_LEFT+"</span>条,已经发送短信<span class=\"f12red\">"+account.NUM_SENDED+"</span>条");
            $("#signature").text(account.SIGNATURE);
            mxChar = $.trim($.FxNull(account.SIGNATURE), "").length;
        }

        function SaveTask() {
            if (!$.validateAll()) {
                if ($(".alxv-failed").size() > 0) {
                    $(document).scrollTop($(".alxv-failed").eq(0).scrollTop());
                }
                return;
            };
            var obj = {
                ScopeCondition: $("#SCOPEPson").attr("checked"),
                ConditionKey: $.trim($("#CONDITION_KEY").val()),
                MobileNums:[],
                Content: $.trim($("#CONTENT").val()),
                Date: $.trim($("#Date").val()),
                Day: $("#Day").val(),
                Week: $("#Week").val(),
                Hour: $("#Hour").val(),
                Minute: $("#Minute").val(),
                SendNow: $("#SendNow").attr("checked"),
                ByTimeStyle:$("#SendByTime").attr("checked")?$("#ByTimeStyle").val():""
            };
            obj.MobileNums=$.trim($("#MOBILE_NUMS").val()).replace("；",";").split(";");
            $.post(handler,{ActionName:"SmsTaskAdd",taskObj:$.toJSON(obj)},function (rslt){
                if(rslt.Success){
                    account=rslt.Data;
                    BindAccInfo();
                    alert("添加成功.");
                    ClearForm();                    
                    return;
                }
                alert(rslt.Message);
            },"json");

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="warrper_body">
            <div class="index">
                <div class="bg_white" style="width: 800px; margin: 16px auto;">
                    <table class="common_table_noborde" width="100%">
                        <tr>
                            <td colspan="2" style="height: 0px;" id="NoAccountMsg"></td>
                        </tr>
                        <tr>
                            <td width="19%" align="right">当前账户信息：
                            </td>
                            <td id="curInfo"></td>
                        </tr>
                        <tr>
                            <td align="right">签名：
                            </td>
                            <td id="signature"></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <label>
                                    <input type="radio" name="condition" value="" id="SCOPEPson" checked="checked" />手工编辑</label>
                                <label>
                                    <input type="radio" name="condition" value="" id="SCOPECondition" />选择条件
                                </label>
                                <select id="CONDITION_KEY" disabled="disabled" class="common_search_select w150">
                                    <option value="">请选择</option>
                                    <%=SmsCondition%>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">手机号：
                            </td>
                            <td>
                                <input type="button" value="" class="btn_add_phone" id="btnSelectMobile" />
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <textarea rows="4" cols="40" id="MOBILE_NUMS" class="cellphone"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">短信模版：
                            </td>
                            <td>
                                <select id="SMS_TEMPLATE" class="w150 common_search_select">
                                    <option>请选择</option>
                                    <%=SmsTemplates%>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <span class="f12red">*</span>短信内容：
                            </td>
                            <td>
                                <textarea rows="4" cols="40" id="CONTENT" maxlength="250"></textarea>
                                <div>当前已输入<span id="wordNum" class="f12red">0</span>个字符,还可输入<span id="wordNumLeft" class="f12red">0</span>个字符.(短信总长度不得超过250个字，超过65个字符将会被拆分成多条发送.)</div>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <label>
                                    <input type="radio" value="0" name="smsType" id="SendNow" checked="checked" />即时发送</label>
                                <label>
                                    <input type="radio" value="1" name="smsType" id="SendByTime" />定时发送</label>
                                <select id="ByTimeStyle" disabled="disabled">
                                    <option value="0">一次性</option>
                                    <option value="1">每天</option>
                                    <option value="2">每周</option>
                                    <option value="3">每月</option>
                                </select>
                                <span id="DateTimeStyle">
                                    <span id="DateCt" style="display: none;">
                                        <input type="text" id="Date" onfocus="WdatePicker()" class="common_search_input w100" />
                                    </span>
                                    <span id="DayCt" style="display: none;">
                                        <select id="Day">
                                            <option value="">请选择</option>
                                        </select>
                                        号
                                    </span>
                                    <span id="WeekCt" style="display: none;">星期
                                        <select id="Week">
                                            <option value="">请选择</option>
                                        </select>
                                    </span>
                                    <span id="HourCt" style="display: none;">
                                        <select id="Hour">
                                            <option value="">请选择</option>
                                        </select>
                                        时
                                    </span>
                                    <span id="MinuteCt" style="display: none;">
                                        <select id="Minute">
                                            <option value="">请选择</option>
                                        </select>
                                        分
                                    </span>
                                </span>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <input type="button" value="" id="btnSave" class="common_btn_save" />
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </form>
</body>
</html>
